/*
 * 
 */
package org.demo.filemanager;

import java.awt.event.KeyEvent;

import org.boxlayout.gui.BoxLayoutEngine;
import org.boxlayout.gui.KeyCustomable;
import org.boxlayout.gui.VisualElement;
import org.boxlayout.gui.layout.LayoutFactory;
import org.boxlayout.gui.type.ColorEnum;
import org.boxlayout.gui.widget.TreeBrowsable;
import org.boxlayout.input.PadManager;
import org.demo.filemanager.widget.filesystem.FileWidgetFactory;


/**
 * TODO: Edit class comment
 * @author dodekane@gmail.com
 */
public class FileManager {
    
    Selector selector = new Selector();
    TreeNavigator navigator ;
    BoxLayoutEngine ble;
    MainFrame mf;
    
    public static void main(String[] args) {
        new FileManager();
    }
    
    public FileManager(){
    	//selector= new Selector();
    	
    	ble = new BoxLayoutEngine();    	
        mf = new MainFrame(ble);
        navigator = new TreeNavigator(ble);
        ble.setMouseEventHandling(true);
        //ble.setMouseEventHandling(false);
        
    	initVisualFile();
    	initKeyboard();
    	initPad();
    }
    
     
	
	void initVisualFile(){
    	
        //ble.getCamera().setZ(2f);
        //FileWidget vf= new FileWidget(ble,"./",selector).setRoot(true).processFinalize();
		TreeBrowsable fileWidget;
		try {
			FileWidgetFactory.getInstance().setBoxLayoutEngine(ble);//.setSelector(selector);
			fileWidget = FileWidgetFactory.getInstance().createFileWidget("/");
			//fileWidget.getVisual().finalizeLayout();
			VisualElement fileSystemRoot = fileWidget.getVisual();
			//fileSystemRoot.setRoot(true);
			//ble.setRootElement(fileSystemRoot);
			
			VisualElement selectArea = ble.createElement();
			
			VisualElement root = ble.createElement().setLayout(LayoutFactory.getInstance().getFloatingLayout()).setColor(ColorEnum.WHITE).setId(0);
			root.setRoot(true);
			root.add(fileSystemRoot);
			root.add(selectArea);
			//root.finalizeLayout();
			ble.setRootElement(root);
			
			//ble.setRootElement();
			//selector.select(fileWidget);
			navigator.setVisual(selectArea);
			navigator.setCurrentNode(fileWidget);
			root.finalizeLayout();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
        //ble.load("text.xml");
        
        //Document doc = DomManager.loadFile("simple.xml");
        
    }
    
	void initKeyboard() {
   	 ble.setOnKey(new KeyCustomable(){

            public void onKeyPressed(KeyEvent e) {
                
                System.out.println("key:"+e.getKeyCode());
                if(e.getKeyCode()==27){//escape
                   escape();
                }else if(e.getKeyCode()==39){
                	right();
                }else if(e.getKeyCode()==37){
                	left();
                } else if(e.getKeyCode()==38){
                	up();
                } else if(e.getKeyCode()==112){
                	displayDebug();
                } else if(e.getKeyCode()==40){
                    down();
                }
            }

			@Override
			public void onKeyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onKeyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}});
	}

	void initPad() {
		PadManager pm = new PadManager();
        pm.init();

		pm.addEventListener(12, 1f, new Runnable(){

			public void run() {
				right();
				
			}
			
		});
		pm.addEventListener(12, -1f, new Runnable(){

			public void run() {
				left();
			}
			
		});
		
		pm.addEventListener(13, -1f, new Runnable(){

			public void run() {
				up();
			}
			
		});
		pm.addEventListener(13, 1f, new Runnable(){

			public void run() {
				down();
			}
			
		});
		pm.start();
		
	}
   
    public void up(){
    	navigator.goToPrevious();

    }
    
    public void down(){
    	navigator.goToNext();

    }
    
    public void left() {
    	navigator.goToParent();

	}
    public void right() {
    	navigator.goToFirstChild();

    }
    
    public void escape() {
    	navigator.goBack();    	
	}
    
    public void displayDebug() {
    	System.out.println(ble.getRootElement().serializeToText(""));    	
	}
    
   
    
}
